package cn.iocoder.yudao.module.system.service.article;

import cn.iocoder.yudao.framework.common.enums.ArticleTypeEnum;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import cn.iocoder.yudao.module.system.controller.admin.article.vo.SystemArticlePageReqVO;
import cn.iocoder.yudao.module.system.controller.admin.article.vo.SystemArticleSaveReqVO;
import cn.iocoder.yudao.module.system.dal.dataobject.article.SystemArticleDO;
import cn.iocoder.yudao.module.system.dal.mysql.article.SystemArticleMapper;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;
import org.springframework.validation.annotation.Validated;

import java.util.List;

import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.module.system.enums.ErrorCodeConstants.*;

/**
 * 弹窗文章 Service 实现类
 *
 * @author admin
 */
@Service
@Validated
public class SystemArticleServiceImpl implements SystemArticleService {

    @Resource
    private SystemArticleMapper articleMapper;

    @Override
    public Long createArticle(SystemArticleSaveReqVO createReqVO) {
        // 类型校验
        if (ArticleTypeEnum.ARTICLE.getType().equals(createReqVO.getType())) {
            // type=0 校验title不能重复
            SystemArticleDO existingArticle = articleMapper.selectOne(SystemArticleDO::getTitle, createReqVO.getTitle());
            if (existingArticle != null) {
                throw exception(ARTICLE_TITLE_DUPLICATE);
            }
        } else if (ArticleTypeEnum.POPUP.getType().equals(createReqVO.getType())) {
            // type=1 校验pagepath不能为空
            if (createReqVO.getPagePath() == null || createReqVO.getPagePath().isEmpty()) {
                throw exception(ARTICLE_PAGEPATH_EMPTY);
            }
            // pagepath+showorder不能重复
            SystemArticleDO existingArticle = articleMapper.selectOne(SystemArticleDO::getPagePath, createReqVO.getPagePath(),
                    SystemArticleDO::getShowOrder, createReqVO.getShowOrder());
            if (existingArticle != null) {
                throw exception(ARTICLE_PAGEPATH_SHOWORDER_DUPLICATE);
            }
        }
        
        // 插入
        SystemArticleDO article = BeanUtils.toBean(createReqVO, SystemArticleDO.class);
        articleMapper.insert(article);
        // 返回
        return article.getId();
    }

    @Override
    public void updateArticle(SystemArticleSaveReqVO updateReqVO) {
        // 校验存在
        validateArticleExists(updateReqVO.getId());
        
        // 类型校验
        if (ArticleTypeEnum.ARTICLE.getType().equals(updateReqVO.getType())) {
            // type=0 校验title不能重复（排除自身）
            SystemArticleDO existingArticle = articleMapper.selectOne(SystemArticleDO::getTitle, updateReqVO.getTitle());
            if (existingArticle != null && !existingArticle.getId().equals(updateReqVO.getId())) {
                throw exception(ARTICLE_TITLE_DUPLICATE);
            }
        } else if (ArticleTypeEnum.POPUP.getType().equals(updateReqVO.getType())) {
            // type=1 校验pagepath不能为空
            if (updateReqVO.getPagePath() == null || updateReqVO.getPagePath().isEmpty()) {
                throw exception(ARTICLE_PAGEPATH_EMPTY);
            }
            // pagepath+showorder不能重复（排除自身）
            SystemArticleDO existingArticle = articleMapper.selectOne(SystemArticleDO::getPagePath, updateReqVO.getPagePath(),
                    SystemArticleDO::getShowOrder, updateReqVO.getShowOrder());
            if (existingArticle != null && !existingArticle.getId().equals(updateReqVO.getId())) {
                throw exception(ARTICLE_PAGEPATH_SHOWORDER_DUPLICATE);
            }
        }
        
        // 更新
        SystemArticleDO updateObj = BeanUtils.toBean(updateReqVO, SystemArticleDO.class);
        articleMapper.updateById(updateObj);
    }

    @Override
    public void deleteArticle(Long id) {
        // 校验存在
        validateArticleExists(id);
        // 删除
        articleMapper.deleteById(id);
    }

    private void validateArticleExists(Long id) {
        if (articleMapper.selectById(id) == null) {
            throw exception(ARTICLE_NOT_EXISTS);
        }
    }

    @Override
    public SystemArticleDO getArticle(Long id) {
        return articleMapper.selectById(id);
    }

    @Override
    public PageResult<SystemArticleDO> getArticlePage(SystemArticlePageReqVO pageReqVO) {
        return articleMapper.selectPage(pageReqVO);
    }

    @Override
    public List<SystemArticleDO> getArticleListByType(Integer type) {
        return articleMapper.selectList(SystemArticleDO::getType, type);
    }

    @Override
    public SystemArticleDO getArticleByTitle(String title) {
        return articleMapper.selectOne(SystemArticleDO::getTitle, title, SystemArticleDO::getType, ArticleTypeEnum.ARTICLE.getType());
    }

}